package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.dto.UserLogin;
import com.neusoft.dto.UserReg;
import com.neusoft.dto.UserUpdate;
import com.neusoft.mapper.UsersMapper;
import com.neusoft.model.Users;
import com.neusoft.service.IUserService;
import com.neusoft.utils.JwtService;
import com.neusoft.utils.UserUtils;
import com.neusoft.vo.RegUserVo;
import com.neusoft.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    JwtService jwtService;

    @Override
    public RegUserVo login(UserLogin userLogin) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("email", userLogin.getEmail());
        map.put("password", userLogin.getPassword());
        queryWrapper.allEq(map);
        Users users = usersMapper.selectOne(queryWrapper);

        if (users != null) {
            User user = new User();
            String token = jwtService.toToken(users);
            user.setEmail(users.getEmail());
            user.setBio(users.getBio());
            user.setUsername(users.getUsername());
            user.setImage(users.getImage());
            user.setToken(token);
            RegUserVo regUserVo = new RegUserVo();
            regUserVo.setUser(user);
            return regUserVo;
        } else {
            return new RegUserVo();
        }
    }

    @Override
    public RegUserVo registerUser(UserReg userReg) {
        //实体类插入
        Users users = new Users();

        users.setEmail(userReg.getEmail());
        users.setUsername(userReg.getUsername());
        users.setPassword(userReg.getPassword());
        usersMapper.insert(users);
        String token = jwtService.toToken(users);
        //返回值
        RegUserVo regUserVo = new RegUserVo();
        User user = new User();
        user.setEmail(users.getEmail());
        user.setUsername(users.getUsername());
        user.setToken(token);
        regUserVo.setUser(user);
        return regUserVo;
    }

    @Override
    public RegUserVo updateUser(UserUpdate userUpdate) {
        Users currentLoginUser = UserUtils.getLoginUser();
        currentLoginUser.setUsername(userUpdate.getUsername());
        currentLoginUser.setBio(userUpdate.getBio());
        currentLoginUser.setEmail(userUpdate.getEmail());
        currentLoginUser.setImage(userUpdate.getImage());
        currentLoginUser.setPassword(userUpdate.getPassword());
        usersMapper.updateById(currentLoginUser);
        String token = jwtService.toToken(currentLoginUser);
        RegUserVo regUserVo = new RegUserVo();
        User user = new User();
        user.setEmail(currentLoginUser.getEmail());
        user.setUsername(currentLoginUser.getUsername());
        user.setBio(currentLoginUser.getBio());
        user.setEmail(currentLoginUser.getEmail());
        user.setImage(currentLoginUser.getImage());
        user.setToken(token);
        regUserVo.setUser(user);
        return regUserVo;
    }
}
